package com.eigaland.change_machine.business.transaction_list

import com.eigaland.change_machine.BuildConfig
import com.eigaland.change_machine.business.network.RetrofitClient
import com.eigaland.change_machine.displaymodel.TransactionListDisplayModel
import com.eigland.eigaland_lib.base.BaseRepository
import com.eigland.eigaland_lib.network.TransactionService
import movie6.posprintercept2.extensions.formatServerDate

public class TransactionListRepository : BaseRepository(){

   suspend fun loadTransactionList(page:Int):TransactionListDisplayModel{

        var result = RetrofitClient.getService(TransactionService::class.java, BuildConfig.domain).getTransactionList(page = page).await()

       var displayModel = TransactionListDisplayModel()

       result?.success?.let {

            var list = arrayListOf<TransactionListItemVM>()
           it.content.map {
               var item = TransactionListItemVM().apply {
                   this.date = it.transaction.createAt.formatServerDate("yyyy/MM/dd HH:mm")
                   this.price = it.transaction.totalPrice.toString()
                   this.paymentType = it.paymentType?.name.toString()
                   this.status = it.transaction.status.name

               }
                list.add(item)
               displayModel.transactionList.addAll(list)

           }

       }

       result?.error?.let {
            displayModel.errorMsg = it.error
       }
       return displayModel

    }
}